// rpc_pipes_server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "rpc_pipes_h.h"
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
	RPC_STATUS status;
	RPC_WSTR pszProtocolSequence = (RPC_WSTR)L"ncacn_np";
	RPC_WSTR pszSecurity         = NULL; 
	RPC_WSTR pszEndpoint         = (RPC_WSTR)L"\\pipe\\hello";
	unsigned int    cMinCalls = 1;
	unsigned int    fDontWait = FALSE;

	status = RpcServerUseProtseqEp(pszProtocolSequence,
		RPC_C_LISTEN_MAX_CALLS_DEFAULT,
		pszEndpoint,
		pszSecurity); 

	if (status) exit(status);

	status = RpcServerRegisterIf(RPC_Pipes_Interface_v1_0_s_ifspec,  
		NULL,   
		NULL); 

	if (status) exit(status);

	status = RpcServerListen(cMinCalls,
		RPC_C_LISTEN_MAX_CALLS_DEFAULT,
		fDontWait);

	if (status) exit(status);
}

void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
	return(malloc(len));
}

void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
	free(ptr);
}

/*
// Pipes code

// In-Pipe

#define PIPE_TRANSFER_SIZE 100 // Transfer 100 pipe elements at one time 
 
void InPipe(LONG_PIPE     long_pipe )
{
    long local_pipe_buf[PIPE_TRANSFER_SIZE];
    ULONG actual_transfer_count = PIPE_TRANSFER_SIZE;
 
    while(actual_transfer_count > 0) // Loop to get all 
                                     //   the pipe data elements
    {
        long_pipe.pull( long_pipe.state,
                        local_pipe_buf,
                        PIPE_TRANSFER_SIZE,
                        &actual_transfer_count);
        //process the elements
    } // end while
} //end InPipe

// Out-Pipe

void OutPipe(LONG_PIPE *outputPipe )
{
    long *outputPipeData;
    ulong index = 0;
    ulong elementsToSend = PIPE_TRANSFER_SIZE;
 
    // Allocate memory for the data to be passed back in the pipe 
    outputPipeData = (long *)malloc( sizeof(long) * PIPE_SIZE );
    
    while(elementsToSend >0) // Loop to send pipe data elements 
    {
        if (index >= PIPE_SIZE)
            elementsToSend = 0;
        else
        {
            if ( (index + PIPE_TRANSFER_SIZE) > PIPE_SIZE )
                elementsToSend = PIPE_SIZE - index;
            else
                elementsToSend = PIPE_TRANSFER_SIZE;
        }
                    
        outputPipe->push( outputPipe->state,
                          &(outputPipeData[index]),
                          elementsToSend ); 
        index += elementsToSend;
 
    } //end while
 
    free((void *)outputPipeData);
 
}
*/